
import { ElMessage, ElLoading, ElMessageBox } from 'element-plus'
const env = import.meta.env.MODE;
const version = "v1.0.1"
//主题颜色
const PRIMARY_COLOR = '#5D6568';



// 取消
const CANCEL_COLOR = '#717171';
console.log('\x1b[32m%s\x1b[0m', `KwaiShou ${version}`);
console.log(env);

const globalVal = (globalThis as any);

// 路径前缀
globalVal.API_URI_PREFIX = "/prod-api"
// API 基础路径
globalVal.API_BASE_PATH = 'http://192.168.2.18:8389'
// 开发环境配置
if (env == 'development') {
  globalVal.API_BASE_PATH = "http://192.168.2.18:8389"
}

// 应用名称
globalVal.APP_NAME = "kwaishou"

// 应用标题
globalVal.APP_TITLE = "KwaiShou"


globalVal.userConfig = {}
try {
  globalVal.userConfig = await globalVal.electronApi.getConfig()
} catch (err) {
  console.error(err)
}

// Toast
globalVal.toast = (message: string) => {
  ElMessage(message)
}

// success
globalVal.success = (message: string) => {
  ElMessage({
    type: 'success',
    message
  })
}

// 错误提示
globalVal.error = (message: string) => {
  ElMessage({
    type: 'error',
    message
  })
}

// Alert
globalVal.alert = (message: string) => {
  ElMessage.error(message)
}


let loadingObj:any = null;
// 显示loading
globalVal.showLoading = (message: string) => {
  loadingObj = ElLoading.service({
    lock: true,
    text: message,
    background: 'rgba(255, 255, 255, .7)'
  })
}

// 关闭loading
globalVal.hideLoading = () => {
  if(loadingObj){
    loadingObj.close()
    loadingObj = null
  }
}



/**
 * 确认框
 */
globalVal.showConfirm = (config: any) => {
  config.confirmButtonColor = config.confirmColor || PRIMARY_COLOR;
  config.cancelButtonColor = config.cancelColor || CANCEL_COLOR;
  config.title = config.title || '提示'
  ElMessageBox({
    title: config.title,
    message: config.message || config.content,
    showCancelButton: true,
    beforeClose: (action, _instance, done) => {
      if (action === 'confirm') {
        if(config.confirm){
          config.confirm(() => {
            done()
          })
        }
        else{
          done();
        }

      } else {
        done()
        config.cancel && config.cancel()
      }

    }
  })
}


/**
 * 解析飞书数据
 * @param record
 * @returns
 */
globalVal.parseFeishuRecord = (record:any) => {
  const fields = record.fields
  const result = {
    recordId: record.record_id
  }
  for(const key in fields){
    let value = fields[key]
    if(Array.isArray(value)){
      value = value[0];
      value = typeof value == 'object' ?  value.text : value;
    }
    else{
      value = typeof value == 'object' ?  value.text : value;
    }
    result[key] = value;
  }
  return result;
}
